'''Game main module.

Contains the entry point used by the run_game.py script.

Feel free to put all your game code here, or in other modules in this "lib"
directory.
'''
import pygame
from spritesheet import *
from board import *
from hero import *
from healthbar import *
from cactus import *
from pygame.locals import *

def main():
	pygame.init()
	screen = pygame.display.set_mode((640, 640))
	clock = pygame.time.Clock()
	pygame.display.set_caption('Melty Twist')
	pygame.mouse.set_visible(0)
	
	spritesheet = Spritesheet()
	board = Board(spritesheet)
	board_group = pygame.sprite.RenderPlain(board)
	
	hero = Hero(spritesheet)
	hero_group = pygame.sprite.RenderPlain(hero)
	
	healthbar = HealthBar(spritesheet)
	healthbar_group = pygame.sprite.RenderPlain(healthbar)
	
	cactus = Cactus(spritesheet)
	cactus_group = pygame.sprite.RenderPlain(cactus)

	board_group.draw(screen)

	while 1:
		clock.tick(60)
		direction = 0
		mx, my = hero.moving
		for event in pygame.event.get():
			if event.type == QUIT:
				return
			elif event.type == KEYDOWN:
				if event.key == K_UP:
					hero.direction = 0
					my = 1
				elif event.key == K_RIGHT:
					hero.direction = 1
					mx = 1
				elif event.key == K_DOWN:
					hero.direction = 2
					my = 1
				elif event.key == K_LEFT:
					hero.direction = 3
					mx = 1
				elif event.key == K_ESCAPE:
					return	
			elif event.type == KEYUP:
				if event.key == K_UP or event.key == K_DOWN:
					mx, my = hero.moving
					my = 0
				elif event.key == K_LEFT or event.key == K_RIGHT:
					mx, my = hero.moving
					mx = 0

		hero.moving = (mx, my)
		hero_group.clear(screen, board.image)
		cactus_group.clear(screen, board.image)
		healthbar_group.clear(screen, board.image)
		
		cactus_group.update()
		cactus_group.draw(screen)
		
		hero_group.update(board.tiles)
		hero_group.draw(screen)
		
		healthbar_group.update(hero.health)
		healthbar_group.draw(screen)
		
		pygame.display.flip()
		hero.health -= 0.05